/*
' DotNetNuke?- http://www.dotnetnuke.com
' Copyright (c) 2002-2006
' by Perpetual Motion Interactive Systems Inc. ( http://www.perpetualmotion.ca )
'
' Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
' documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
' the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
' to permit persons to whom the Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in all copies or substantial portions 
' of the Software.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
' DEALINGS IN THE SOFTWARE.
 */

using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Web.UI;
using System.Web.UI.WebControls;

using DotNetNuke;
using DotNetNuke.Common;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Modules.Actions;
using DotNetNuke.Entities.Users;
using DotNetNuke.Security;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Services.Localization;
using DotNetNuke.Services.FileSystem;
using DotNetNuke.Security.Roles;
using DotNetNuke.Security.Membership;
using System.Text;
using Arjones.Controls;

namespace M2Land.Modules.FlashImageRotator
{
    /// -----------------------------------------------------------------------------
    /// <summary>
    /// The ViewFlashImageRotator class displays the content
    /// </summary>
    /// <remarks>
    /// </remarks>
    /// <history>
    /// </history>
    /// -----------------------------------------------------------------------------
    partial class ViewFlashImageRotator : PortalModuleBase, IActionable
    {

        #region Private Members


        #endregion

        #region Public Methods

        public bool DisplayAudit()
        {
            bool retValue = false;

            if ((string)Settings["auditinfo"] == "Y")
            {
                retValue = true;
            }

            return retValue;
        }

        #endregion

        #region Event Handlers

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Page_Load runs when the control is loaded
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <history>
        /// </history>
        /// -----------------------------------------------------------------------------
        protected void Page_Load(System.Object sender, System.EventArgs e)
        {

            const string STR_FlashFilePath = "Flash/imagerotator.swf";

            try
            {
                //Initiate Falash object 
                swfImageRotator.FlashUrl = ModulePath + STR_FlashFilePath;
                swfImageRotator.Width = 470;
                swfImageRotator.Height = 160;
                swfImageRotator.FlashParams.Add("wmode", "transparent");
                swfImageRotator.FlashParams.Add("enablejs", "false");
                swfImageRotator.FlashParams.Add("allowfullscreen", "true");
                swfImageRotator.FlashParams.Add("allowscriptaccess", "always");
                swfImageRotator.FlashVars.Add("file", ModulePath + "Flash/ImageList.xml");
                swfImageRotator.FlashVars.Add("transition", "random");
                swfImageRotator.FlashVars.Add("logo", "http://www.aboutdnn.com/Portals/0/aboutdnnlogo.gif");

                if (!Page.IsPostBack)
                {
                    FlashImageRotatorInfo objFlashImageRotatorInfo = new FlashImageRotatorInfo();
                    FlashImageRotatorController objFlashImageRotatorController = new FlashImageRotatorController();
                    objFlashImageRotatorInfo = objFlashImageRotatorController.GetFlashImageRotator(ModuleId);

                    //If has customized settings
                    if (objFlashImageRotatorInfo != null)
                    {
                        AddFlashVar(swfImageRotator, objFlashImageRotatorInfo);
                    }
                }
            }
            catch (Exception exc) //Module failed to load
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }

        }
        #endregion

        private void AddFlashVar(SWFObject swfObject, FlashImageRotatorInfo objFlashImageRotatorInfo)
        {
            if (objFlashImageRotatorInfo.Width != Null.NullInteger)
            {
                swfObject.FlashVars.Add("width", objFlashImageRotatorInfo.Width.ToString());
                Unit width = new Unit(objFlashImageRotatorInfo.Width.ToString());
                swfObject.Width = width;
            }

            if (objFlashImageRotatorInfo.Height != Null.NullInteger)
            {
                swfObject.FlashVars.Add("height", objFlashImageRotatorInfo.Height.ToString());
                Unit height = new Unit(objFlashImageRotatorInfo.Height.ToString());
                swfObject.Height = height;
            }

            if (objFlashImageRotatorInfo.File != Null.NullString)
            {
                swfImageRotator.FlashVars.Remove("file");
                swfImageRotator.FlashVars.Add("file", ModulePath + "Flash/" + objFlashImageRotatorInfo.File);
            }

            if (objFlashImageRotatorInfo.Transtion.Trim() != Null.NullString && objFlashImageRotatorInfo.Transtion.Trim() != "random")
            {
                swfObject.FlashVars.Add("transition", objFlashImageRotatorInfo.Transtion.Trim());
            }

            //The colors
            if (objFlashImageRotatorInfo.BackColor.Trim() != Null.NullString)
            {
                swfObject.FlashVars.Add("backcolor", objFlashImageRotatorInfo.BackColor.Replace("#", "0x").Trim() );
            }

            if (objFlashImageRotatorInfo.FrontColor.Trim() != Null.NullString)
            {
                swfObject.FlashVars.Add("frontcolor", objFlashImageRotatorInfo.FrontColor.Replace("#", "0x").Trim() );
            }

            if (objFlashImageRotatorInfo.LightColor.Trim() != Null.NullString)
            {
                swfObject.FlashVars.Add("lightcolor", objFlashImageRotatorInfo.LightColor.Replace("#", "0x").Trim() );
            }

            if (objFlashImageRotatorInfo.ScreenColor.Trim() != Null.NullString)
            {
                swfObject.FlashVars.Add("screencolor", objFlashImageRotatorInfo.ScreenColor.Replace("#", "0x").Trim() );
            }

            //Display appearance
            if (objFlashImageRotatorInfo.Logo.Trim() != Null.NullString)
            {


                if (objFlashImageRotatorInfo.Logo.IndexOf("FileID") < 0)
                {
                    swfObject.FlashVars.Add("logo", objFlashImageRotatorInfo.Logo );
                }
                else
                {

                    String strLogoFileID = objFlashImageRotatorInfo.Logo.Substring(7);
                    int intLogoFileID = int.Parse(strLogoFileID);

                    FileInfo objFileInfo = new FileInfo();
                    FileController objFileController = new FileController();

                    objFileInfo = objFileController.GetFileById(intLogoFileID, PortalId);

                    String strLogoFilePath = PortalSettings.HomeDirectory + objFileInfo.Folder + objFileInfo.FileName;

                    swfObject.FlashVars.Add("logo", strLogoFilePath );
                }

            }

            if (objFlashImageRotatorInfo.OverStrech.Trim() != "false")
            {
                swfObject.FlashVars.Add("overstretch", objFlashImageRotatorInfo.OverStrech.Trim() );
            }

            if (objFlashImageRotatorInfo.ShowIcons == false)
            {
                swfObject.FlashVars.Add("showicon", objFlashImageRotatorInfo.ShowIcons.ToString() );
            }


            if (objFlashImageRotatorInfo.ShowEq == true)
            {
                swfObject.FlashVars.Add("showeq", objFlashImageRotatorInfo.ShowEq.ToString().ToLower() );
            }

            //Contrlbar appearance
            if (objFlashImageRotatorInfo.ShowNavigation == false)
            {
                swfObject.FlashVars.Add("shownavigation", objFlashImageRotatorInfo.ShowNavigation.ToString() );
            }

            //Playback Behaviour
            if (objFlashImageRotatorInfo.Audio.Trim() != Null.NullString)
            {


                if (objFlashImageRotatorInfo.Audio.IndexOf("FileID") < 0)
                {
                    swfObject.FlashVars.Add("audio", objFlashImageRotatorInfo.Audio );
                }
                else
                {

                    String strAudioFileID = objFlashImageRotatorInfo.Audio.Substring(7);
                    int intAudioFileID = int.Parse(strAudioFileID);

                    FileInfo objFileInfo = new FileInfo();
                    FileController objFileController = new FileController();

                    objFileInfo = objFileController.GetFileById(intAudioFileID, PortalId);

                    if (objFileInfo != null)
                    {

                        String strAudioFilePath = PortalSettings.HomeDirectory + objFileInfo.Folder + objFileInfo.FileName;

                        swfObject.FlashVars.Add("audio", strAudioFilePath );
                    }
                }

            }

            if (objFlashImageRotatorInfo.AutoStart != true)
            {
                swfObject.FlashVars.Add("autostart", objFlashImageRotatorInfo.AutoStart.ToString().ToLower() );
            }

            if (objFlashImageRotatorInfo.Repeat.Trim() != Null.NullString && objFlashImageRotatorInfo.Repeat.Trim().ToLower() != "false")
            {
                swfObject.FlashVars.Add("repeat", objFlashImageRotatorInfo.Repeat );
            }

            if (objFlashImageRotatorInfo.RotateTime != Null.NullInteger)
            {
                swfObject.FlashVars.Add("rotatetime", objFlashImageRotatorInfo.RotateTime.ToString() );
            }

            if (objFlashImageRotatorInfo.Shuffle != false)
            {
                swfObject.FlashVars.Add("shuffle", objFlashImageRotatorInfo.Shuffle.ToString() );
            }

            if (objFlashImageRotatorInfo.Volume != Null.NullInteger)
            {
                swfObject.FlashVars.Add("volume", objFlashImageRotatorInfo.Volume.ToString() );
            }

            //External Commnunication:
            if (objFlashImageRotatorInfo.EnableJS != true)
            {
                swfObject.FlashVars.Add("enablejs", objFlashImageRotatorInfo.EnableJS.ToString() );
            }

            if (objFlashImageRotatorInfo.JavascriptID.Trim() != Null.NullString)
            {
                swfObject.FlashVars.Add("javascriptid", objFlashImageRotatorInfo.JavascriptID );
            }

            if (objFlashImageRotatorInfo.LinkFromDisplay != false)
            {
                swfObject.FlashVars.Add("linkfromdisplay", objFlashImageRotatorInfo.LinkFromDisplay.ToString() );
            }

            if (objFlashImageRotatorInfo.LinkTarget.Trim() != Null.NullString)
            {
                swfObject.FlashVars.Add("linktarget", objFlashImageRotatorInfo.LinkTarget );
            }

        }

        #region Optional Interfaces

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Registers the module actions required for interfacing with the portal framework
        /// </summary>
        /// <value></value>
        /// <returns></returns>
        /// <remarks></remarks>
        /// <history>
        /// </history>
        /// -----------------------------------------------------------------------------
        public ModuleActionCollection ModuleActions
        {
            get
            {
                ModuleActionCollection Actions = new ModuleActionCollection();
                Actions.Add(this.GetNextActionID(), Localization.GetString(ModuleActionType.AddContent, this.LocalResourceFile), ModuleActionType.AddContent, "", "", this.EditUrl(), false, SecurityAccessLevel.Edit, true, false);
                return Actions;
            }
        }

        #endregion

    }
}

